<template>
	<view style="padding: 20upx 30upx;">

			<u--textarea v-model="value" placeholder="请输入内容"></u--textarea>
			<view class="" style="height: 20rpx;">
				
			</view>
<u-upload
		:fileList="fileList1"
		@afterRead="afterRead"
		@delete="deletePic"
		name="1"
		multiple
		:maxCount="3"
	></u-upload>

		<view class="btn" @click="submit">
			提交
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: '',
				fileList1: [],
			}
		},
		methods: {
			// 删除图片
						deletePic(event) {
							this[`fileList${event.name}`].splice(event.index, 1)
						},
						// 新增图片
						async afterRead(event) {
							// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
							let lists = [].concat(event.file)
							let fileListLen = this[`fileList${event.name}`].length
							lists.map((item) => {
								this[`fileList${event.name}`].push({
									...item,
									status: 'uploading',
									message: '上传中'
								})
							})
							for (let i = 0; i < lists.length; i++) {
								const result = await this.uploadFilePromise(lists[i].url)
								let item = this[`fileList${event.name}`][fileListLen]
								this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
									status: 'success',
									message: '',
									url: result
								}))
								fileListLen++
							}
						},
						uploadFilePromise(url) {
							return new Promise((resolve, reject) => {
								let a = uni.uploadFile({
									url: 'https://wjlm.goldsu.cn/home/upload/upload', // 仅为示例，非真实的接口地址
									filePath: url,
									name: 'file',
									formData: {
										user: 'test'
									},
									success: (res) => {
										setTimeout(() => {
											resolve(JSON.parse(res.data).data)
										}, 1000)
									}
								});
							})
						},
			submit() {
				let pic=[]
				this.fileList1.forEach(item=>{
					pic.push(item.url)
				})
				this.$myRuquest('home/user/message_save', {
					content: this.value,
					pic:pic.join(',')
				},'POST').then(res => {
					uni.showToast({
						title: '提交成功',
						icon: 'none',
						success() {
							setTimeout(()=>{
								uni.navigateBack({
									delta: 1
								})
							},1500)
						}
					});

				})
			}
		}
	}
</script>

<style>
	.btn {
		height: 90upx;
		color: #AC7D3D;
		background-color: #FED194;
		border-radius: 90upx;
		line-height: 90upx;
		text-align: center;
		margin-top: 50upx;
	}
</style>
